package psn.zjk.exporter;

import psn.zjk.exporter.utils.Assert;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author sdyt_zhangjunkai
 * @version 1.0
 * @CLASS_NAME DynamicTableBuilder
 * @DESCRIPTION
 * @CREATE_TIME 2020/9/16 14:14
 */
public class FixedHeaderTableBuilder extends ExcelDataBuilder {

    /**
     * @METHOD_NAME: ExcelDataBuilder
     * @DESCRIPTION: 固定列头
     * @CREATE_TIME 2020/9/4 14:45
     * @RETURN
     * @AUTHOR sdyt_zhangjunkai
     */
    protected FixedHeaderTableBuilder(String rowHeaderField, List<String> fixedHeaders) {
        super();
        Assert.hasLength(rowHeaderField, "RowHeaderField is empty!");
        Assert.notEmpty(fixedHeaders, "FixedHeaders is null!");
        //列头
        excelData.fixedHeaders = fixedHeaders;
        //行头
        excelData.rowHeaderField = rowHeaderField;
    }

    @Override
    protected void assembleDatas() {
        // 头列
        SheetColumn headerSheetColumn = column(excelData.firstColumn);

        SheetRow sheetRow = null;
        //行号
        int rowIndex = excelData.firstRow;
        for (Object data : excelData.datas) {// 一条数据一行
            //获取当前行
            if (excelData.rowHeaderField != null) {//行需要设置数据
                // 行头
                String rowValue = getPropertyWithConvertNull(data, excelData.rowHeaderField).toString();
                sheetRow = uniqueRows.get(rowValue);
            } else {//行不需设置数据
                sheetRow = row(rowIndex++);
            }
            //获取列
            for (SheetColumn sheetColumn : excelData.sheetColumns.values()) {//一个列头一列
                //表数据
                if (sheetColumn.getHeader()==null){
                    continue;
                }
                Object value = getMappedValue(data, sheetColumn.getHeader());
                cell(sheetRow, sheetColumn, value);
            }
        }
    }


    @Override
    protected void assembleTmpPojo(SheetRow row, Object pojo) {
        for (SheetColumn sheetColumn : excelData.sheetColumns.values()) {
            Object value = getMappedValue(pojo, sheetColumn.getHeader());
            cell(row, sheetColumn, value);
        }
    }

    @Override
    protected List<String> getHeaders() {
        return excelData.fixedHeaders;
    }

    @Override
    protected List<String> getRowHeaders() {
        List<String> list = null;
        if (mergeRepeat) {
            HashSet<String> rowHeaders = new HashSet<>();
            excelData.datas.forEach(o -> {
                String rowheader = getPropertyWithConvertNull(o, excelData.rowHeaderField).toString();
                rowHeaders.add(rowheader);
            });
            list = new ArrayList<>(rowHeaders);
        } else if (excelData.rowHeaderField != null) {
            list = new ArrayList<>();
            for (Object data : excelData.datas) {
                String rowheader = getPropertyWithConvertNull(data, excelData.rowHeaderField).toString();
                list.add(rowheader);
            }
        }
        return list;
    }


}
